package com.example.innelop

import org.junit.Test


/**
 * 测试内置函数takeInf
 * 特征：返回调用者本身。满足条件则执行函数体
 *
 * @author zhouronghua
 * @time 2022/3/24 9:27 下午
 */
class takeIfTest {

    @Test
    fun main() {
        val result = checkPermissionAction("Root2", "!@#$")
        if (result != null) {
            println("欢迎${result}尊贵的用户登录系统, 拥有超级权限")
        } else {
            println("您的权限不够")
        }
        println(checkPermissionAction2("Root", "!@#$"))
    }

    private fun checkPermissionAction(name: String, pwd: String): String? {
        return name.takeIf { permissionCheck(name, pwd) }
    }

    private fun checkPermissionAction2(name: String, pwd: String): String? {
        // name.takeIf返回结果直接返回name本身，否则返回空合并操作符内容
        return name.takeIf {
            permissionCheck(name, pwd)
        } ?: "你的权限不够"
    }

    /**
     * 用户权限检查
     *
     */
    private fun permissionCheck(name: String, pwd: String): Boolean {
        return name == "Root" && pwd == "!@#$"
    }
}